import axios from 'axios'
import commonUrl from '../common/js/commonUrl.js'
import { loadOpenId, loadVillageId, loadUserId } from '../common/js/cache.js'
import qs from 'qs'

const url = `${commonUrl.apihost}index.php/home/`

//  头像资料数据
export function getMyInfo () {
  const url = `${commonUrl.apihost}index.php/home/login/getUser`
  const data = {
    openid: loadOpenId(),
    userId: loadUserId()
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 获取验证码
export function getPhoneCode (phoneNumber) {
  return axios({
    method: 'GET',
    url: `${url}login/code?phone=${phoneNumber}`
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 保存编辑资料
export function saveEditData (name, realname, phone, sex, identity, code, email, realauth) {
  const data = {
    name,
    real_name: realname,
    phone,
    sex,
    wx_role: identity,
    village_id: loadVillageId(),
    code,
    userId: loadUserId(),
    email,
    real_auth: realauth
  }
  return axios({
    method: 'POST',
    url: `${url}login/perfection`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 我的房列表
export function getDatas () {
  const data = {
    userId: loadUserId()
  }
  return axios({
    method: 'POST',
    url: `${url}house_Auth/getMyHouse`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 我的房屋的重新匹配
export function rematch (name, tel) {
  const data = {
    phone: tel,
    userId: loadUserId(),
    real_name: name
  }
  return axios({
    method: 'POST',
    url: `${commonUrl.apihost}index.php/home/login/resethouse`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 房屋认证结果页面发送验证码
export function sendResrveCode (phone) {
  const data = {
    userId: loadUserId(),
    phone: phone
  }
  return axios({
    method: 'POST',
    url: `${commonUrl.apihost}index.php/home/house_Auth/houseVerifyCode`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 房屋认证结果页面的保存
export function keepHouseInfo (id, phone, code) {
  const data = {
    userId: loadUserId(),
    id: id,
    phone: phone,
    code: code
  }
  return axios({
    method: 'POST',
    url: `${commonUrl.apihost}index.php/home/house_Auth/verifyHouse`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 房屋认证  小区  苑  幢  单元  房号
export function getHouseData (id) {
  return axios({
    method: 'GET',
    url: `${url}house_Auth/getHouse?id=${id}`
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 保存房屋资料
export function saveHouseData (houseid, areaid, phoneNum, code) {
  const data = {
    openid: loadOpenId(),
    house_id: houseid,
    areas_id: areaid,
    user_id: loadUserId(),
    phone: phoneNum,
    code: code
  }
  return axios({
    method: 'POST',
    url: `${url}house_Auth/setHouse`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 我的查验列表
export function getCheckList () {
  return axios({
    method: 'GET',
    url: `${url}examine/myExamineIndex/?user_id=${loadUserId()}`
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 改变我的查验列表的未完成状态
export function setFinish (id, degree) {
  const data = {
    id: id,
    is_finish: degree
  }
  return axios({
    method: 'POST',
    url: `${url}examine/setFinish`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 删除某条查验内容
export function delCheck (id) {
  return axios({
    method: 'GET',
    url: `${url}examine/deleteExamine/?id=${id}`
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 编辑查验内容
export function editCheckContent (id, categoryid) {
  const data = {
    id: id,
    category_id: categoryid
  }
  return axios({
    method: 'POST',
    url: `${url}examine/firstAssortInfo`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 编辑查验内容
export function editCheckEvery (id, categoryid) {
  const data = {
    id: id,
    category_id: categoryid
  }
  return axios({
    method: 'POST',
    url: `${url}examine/secondLowCateInfo`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 提交编辑查验内容
export function submitEditCheckEvery (id, summary) {
  const data = {
    related_id: id,
    summary: summary
  }
  return axios({
    method: 'POST',
    url: `${url}examine/editSummary`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 房屋认证页面电话号码获取接口
export function getHousePhone (id) {
  const data = {
    houseId: id
  }
  return axios({
    method: 'POST',
    url: `${url}house_auth/getHousePhone`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 房屋认证验证码
export function getHouseCode (phone) {
  const data = {
    phone: phone,
    userId: loadUserId()
  }
  return axios({
    method: 'POST',
    url: `${url}house_auth/HouseCode`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 房屋审核列表
export function HomeAuditList(num, key) {
  const url = `${commonUrl.apihost}index.php/home/Proprietorauth/index`
  const data = {
    user_id: loadUserId(),
    status: num,
    name: key
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 房屋审核列表的详情
export function AuditListDetail (id) {
  const url = `${commonUrl.apihost}index.php/home/Proprietorauth/detail`
  const data = {
    id: id
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 房屋审核详情页面的拒绝和通过
export function AuditPost (id, orAndNot, room, msg, style) {
  const url = `${commonUrl.apihost}index.php/home/Proprietorauth/auditing`
  const data = {
    id: id,
    process: orAndNot,
    name: room,
    message: msg,
    h_type: style
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 扫码送票部分的接口
export function searchScan (keyword) {
  const url = `${commonUrl.apihost}index.php/home/house_auth/searchhouse`
  const data = {
    keyword: keyword
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

export function bindPost (qrcode, hid, status) {
  const url = `${commonUrl.apihost}index.php/home/house_auth/bindQrcode`
  const data = {
    qrcode: qrcode,
    house_ids: hid,
    status: status
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 图片轮播接口
export function carPicture (num) {
  const url = 'https://xsfgwyw.zgwyzxw.cn/api/banner/list'
  const data = {
    type: num // 1:业主  2：邻里
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 判断是否可以进行体验申请
export function whetherApply () {
  const url = `${commonUrl.apihost}index.php/home/login/getApplyTestHouse`
  const data = {
    user_id: loadUserId()
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 提交申请体验
export function postApply (name, tel, codeNum) {
  const url = `${commonUrl.apihost}index.php/home/login/applyTestHouse`
  const data = {
    real_name: name,
    phone: tel,
    code: codeNum,
    user_id: loadUserId()
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 查验报价
export function checkOffer (area, fire, elevator) {
  const url = 'https://xsfgwyw.zgwyzxw.cn//api/price/list'
  const data = {
    area: area,
    fire: fire,
    elevator: elevator
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 跑楼申请-添加申请
export function addApply (aid, content, nid) {
  const url = `${commonUrl.apihost}index.php/home/Votestatistics/applyRunVote`
  const data = {
    areas_id: aid,
    user_id: loadUserId(),
    content: content,
    project_id: nid
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 跑楼项目经理列表获取
export function projectManager () {
  const url = `${commonUrl.apihost}index.php/home/Votestatistics/getProjectManager`
  const data = {
    user_id: loadUserId()
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 跑楼申请-申请记录
export function applyRecord () {
  const url = `${commonUrl.apihost}index.php/home/Votestatistics/getApplyRecord`
  const data = {
    user_id: loadUserId()
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 跑楼审核列表
export function buildAudit (number, aname) {
  const url = `${commonUrl.apihost}index.php/home/Votestatistics/getRunVoteApplyList`
  const data = {
    user_id: loadUserId(),
    areas_name: aname,
    status: number
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 跑楼审核中的拒绝和通过
export function handleRun (id, number, time) {
  const url = `${commonUrl.apihost}index.php/home/Votestatistics/auditRunVote`
  const data = {
    id: id,
    user_id: loadUserId(),
    status: number,
    end_time: time
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 工时统计列表
export function workTimeList (key) {
  const url = `${commonUrl.apihost}index.php/home/Votestatistics/getCheckRecord`
  const data = {
    user_id: loadUserId(),
    key: key
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 打卡记录
export function punchList (id) {
  const url = `${commonUrl.apihost}index.php/home/Votestatistics/getCheckRecordDetail`
  const data = {
    id: id
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 工时统计保存
export function keepTime (id, time, text) {
  const url = `${commonUrl.apihost}index.php/home/Votestatistics/saveWorkHours`
  const data = {
    id: id,
    hours: time,
    user_id: loadUserId(),
    remark: text
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}
